package com.corona.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.corona.Game;
import com.corona.game.util.TextureCache;
import com.corona.game.util.audio.AudioLoader;
import java.util.ArrayList;
import java.util.Iterator;

/* compiled from: Action.java */
/* access modifiers changed from: package-private */
/* loaded from: pokewilds.jar:com/pkmngen/game/DisplayTextIntro.class */
public class DisplayTextIntro extends Action {
    Sprite arrowSprite;
    Sprite arrowSprite2;
    Action playSoundAction;
    boolean playSound;
    Action scrollUpAction;
    Sprite helperSprite;
    Sprite bgSprite;
    int timer;
    int speedTimer;
    int speed;
    Action triggerAction;
    boolean exitWhenDone;
    int charsPerLine = 32;
    int spacing = 6;
    public Action.Layer layer = Action.Layer.gui_110;
    Vector3 touchLoc = new Vector3();
    Vector2 touchLoc2d = new Vector2();
    boolean firstStep = true;
    boolean waitingOnExit = false;
    boolean foundTrigger = false;
    boolean checkTrigger = false;
    ArrayList<Sprite> spritesNotDrawn = new ArrayList<>();
    ArrayList<Sprite> spritesBeingDrawn = new ArrayList<>();

    public DisplayTextIntro(Game game, String textString, String playSound, Action triggerAction, boolean exitWhenDone, Action nextAction) {
        super(new Object[0]);
        this.exitWhenDone = true;
        this.nextAction = nextAction;
        this.exitWhenDone = exitWhenDone;
        this.triggerAction = triggerAction;
        if (playSound != null) {
            this.playSoundAction = new PlaySoundText(playSound, null);
            this.playSound = true;
        } else {
            this.playSound = false;
        }
        this.speed = 3;
        this.speedTimer = this.speed;
        String line = "";
        String lines = "";
        String[] words = textString.split(" ");
        for (String word : words) {
            if (line.length() + word.length() < 18) {
                line = line + word;
                if (line.length() != 17) {
                    line = line + " ";
                }
            } else {
                while (line.length() < 18) {
                    line = line + " ";
                }
                lines = lines + line;
                line = word + " ";
            }
        }
        int i = 0;
        int j = 0;
        for (char letter : (lines + line).toCharArray()) {
            Sprite letterSprite = game.textDict.get(Character.valueOf(letter));
            Sprite currSprite = new Sprite(letterSprite == null ? game.textDict.get(null) : letterSprite);
            currSprite.setPosition((float) (((10 + (8 * i)) + 2) - 4), (float) (((26 - (16 * j)) + 2) - 4));
            this.spritesNotDrawn.add(currSprite);
            if (i >= 17) {
                i = 0;
                j++;
            } else {
                i++;
            }
        }
        this.arrowSprite = new Sprite(TextureCache.get(Gdx.files.internal("arrow_down.png")), 0, 0, 7, 5);
        this.arrowSprite.setPosition(144.0f, 10.0f);
        this.timer = 0;
        this.bgSprite = new Sprite(TextureCache.get(Gdx.files.internal("textbox_bg1.png")), 0, 0, 160, 144);
    }

    @Override // com.corona.game.Action
    public String getCamera() {
        return "gui";
    }

    @Override // com.corona.game.Action
    public Action.Layer getLayer() {
        return this.layer;
    }

    @Override // com.corona.game.Action
    public void step(Game game) {
        if (this.firstStep) {
            game.displayTextAction = this;
            this.firstStep = false;
        }
        this.bgSprite.draw(game.uiBatch);
        Iterator<Sprite> it = this.spritesBeingDrawn.iterator();
        while (it.hasNext()) {
            it.next().draw(game.uiBatch);
        }
        if (this.waitingOnExit || game.actionStack.contains(this.scrollUpAction) || game.actionStack.contains(this.playSoundAction)) {
            return;
        }
        if (this.checkTrigger) {
            if (game.actionStack.contains(this.triggerAction)) {
                this.foundTrigger = true;
            } else if (this.foundTrigger) {
                game.actionStack.remove(this);
            }
        } else if (this.spritesBeingDrawn.size() >= 36 || this.spritesNotDrawn.isEmpty()) {
            if (this.playSound && this.spritesNotDrawn.isEmpty()) {
                game.insertAction(this.playSoundAction);
                this.playSoundAction.step(game);
                this.playSound = false;
            } else if (this.triggerAction != null) {
                game.insertAction(this.nextAction);
                this.checkTrigger = true;
            } else if (this.spritesNotDrawn.isEmpty()) {
                game.insertAction(this.nextAction);
                if (this.exitWhenDone) {
                    game.actionStack.remove(this);
                } else {
                    this.waitingOnExit = true;
                }
            } else {
                this.scrollUpAction = new ScrollTextUp(game, this.spritesBeingDrawn, this.spritesNotDrawn);
                game.insertAction(this.scrollUpAction);
            }
        } else if (this.speedTimer > 0) {
            this.speedTimer--;
        } else {
            this.speedTimer = this.speed;
            for (int i = 0; i < 1 && !this.spritesNotDrawn.isEmpty() && this.spritesBeingDrawn.size() < 36; i++) {
                this.spritesBeingDrawn.add(this.spritesNotDrawn.remove(0));
            }
        }
    }

    /* compiled from: Action.java */
    /* access modifiers changed from: package-private */
    /* loaded from: pokewilds.jar:com/pkmngen/game/DisplayTextIntro$PlaySoundText.class */
    public class PlaySoundText extends Action {
        Music music;
        float initialVolume;
        boolean playedYet = false;

        /* JADX INFO: 'super' call moved to the top of the method (can break code semantics) */
        public PlaySoundText(String sound, Action nextAction) {
            super(new Object[0]);
            DisplayTextIntro.this = this$0;
            this.nextAction = nextAction;
            String path = "";
            float volume = 1.0f;
            if (!sound.contains("cries")) {
                path = "sounds/";
            } else {
                volume = 0.5f;
            }
            this.music = AudioLoader.loadMusic(path + sound);
            this.music.setLooping(false);
            this.music.setVolume(volume);
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            if (this.music != null && !this.playedYet) {
                this.music.play();
                this.playedYet = true;
            }
            if (!this.music.isPlaying()) {
                game.actionStack.remove(this);
                game.insertAction(this.nextAction);
            }
        }
    }

    /* compiled from: Action.java */
    /* loaded from: pokewilds.jar:com/pkmngen/game/DisplayTextIntro$ScrollTextUp.class */
    class ScrollTextUp extends Action {
        Vector2 position;
        ArrayList<Sprite> text;
        ArrayList<Sprite> otherText;
        public Action.Layer layer = Action.Layer.gui_110;
        ArrayList<Vector2> positions = new ArrayList<>();

        /* JADX INFO: 'super' call moved to the top of the method (can break code semantics) */
        public ScrollTextUp(Game game, ArrayList<Sprite> text, ArrayList<Sprite> otherText) {
            super(new Object[0]);
            DisplayTextIntro.this = this$0;
            this.text = text;
            this.otherText = otherText;
            for (int i = 0; i < 5; i++) {
                this.positions.add(new Vector2(0.0f, 0.0f));
            }
            this.positions.add(new Vector2(0.0f, 8.0f));
            for (int i2 = 0; i2 < 5; i2++) {
                this.positions.add(new Vector2(0.0f, 0.0f));
            }
            this.positions.add(new Vector2(0.0f, 8.0f));
        }

        @Override // com.corona.game.Action
        public String getCamera() {
            return "gui";
        }

        @Override // com.corona.game.Action
        public Action.Layer getLayer() {
            return this.layer;
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            this.position = this.positions.get(0);
            this.positions.remove(0);
            Iterator<Sprite> it = this.text.iterator();
            while (it.hasNext()) {
                Sprite sprite = it.next();
                sprite.setPosition(sprite.getX() + this.position.x, sprite.getY() + this.position.y);
            }
            Iterator<Sprite> it2 = this.otherText.iterator();
            while (it2.hasNext()) {
                Sprite sprite2 = it2.next();
                sprite2.setPosition(sprite2.getX() + this.position.x, sprite2.getY() + this.position.y);
            }
            if (this.positions.isEmpty()) {
                for (int i = 0; i < 18; i++) {
                    this.text.remove(0);
                }
                game.actionStack.remove(this);
            }
        }
    }
}
